package com.ww.springboot.boot.javabase.函数式编程;

import org.apache.poi.ss.formula.functions.T;

import java.util.function.*;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2023-02-27 17:22
 */
public class Demo1 {

    public static void main(String[] args) {
        String sss = "s";
        String ssss = "ss";

        System.out.println(String.format("使用predicate判断 %s 并返回 %s", sss, predicate(sss, s -> s.equals("s"))));//判断并返回
        System.out.println(String.format("function 加工%s 并返回%s:", sss, function(sss, s -> s + "1")));
        System.out.println(String.format("使用supplier生产 %s", supplier(() -> sss)));//生产
        consumer(sss, s -> System.out.println(String.format("使用consumer消费 %s,无返回", s)));//消费不返回
        System.out.println(String.format("一元操作 %s -> %s", sss, unaryOperator(sss, s -> s + "" + s)));
        System.out.println(String.format("二元操作 %s * %s -> %s", sss, ssss, binaryOperator(sss, ssss, (s, s1) -> s + "" + s1)));
    }

    private static boolean predicate(String s, Predicate predicate) {
        return predicate.test(s);
    }

    private static <T, R> R function(T s, Function<T, R> function) {
        return function.apply(s);
    }

    private static <T> T supplier(Supplier<T> supplier) {
        return supplier.get();
    }

    private static void consumer(String ss, Consumer consumer) {
        consumer.accept(ss);
    }

    private static <T> T unaryOperator(T ss, UnaryOperator<T> unaryOperator) {
        return unaryOperator.apply(ss);
    }

    private static <T> T binaryOperator(T ss, T ss2, BinaryOperator<T> binaryOperator) {
        return binaryOperator.apply(ss, ss2);
    }
}
